package agenda;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.media.Manager;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Player.java
 *
 * Created on 12/11/2011, 10:52:22
 */

/**
 *
 * @author Brenno
 */
public class Player extends javax.swing.JFrame {

    /** Creates new form Player */
    public Player(Anotação Anotacao) {
    	this.Anotacao = Anotacao;
    	iniciaAudioPlayer();
    	initComponents();
    	setDefaultCloseOperation(HIDE_ON_CLOSE);
        setIconImage((new ImageIcon(getClass().getResource("/agenda/logo.png")).getImage()));
        setTitle("Player");
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
    	
        jPanel1 = new javax.swing.JPanel();
        jLabelAnotação = new javax.swing.JLabel();
        jLabelNome = new javax.swing.JLabel();
        Nome = new javax.swing.JLabel();
        jButtonPlay = new javax.swing.JButton();
        jButtonStop = new javax.swing.JButton();
        jButtonPause = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelAnotação.setFont(new java.awt.Font("Monotype Corsiva", 1, 24)); // NOI18N
        jLabelAnotação.setText("Anotação de Áudio\n");
        Nome.setFont(new java.awt.Font("Times New Roman", 1, 14));
        Nome.setText(Anotacao.getNome());
        
        jLabelNome.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabelNome.setText("Nome:");

        jButtonPlay.setIcon(new ImageIcon(getClass().getResource("/agenda/Player Play.png"))); // NOI18N
        jButtonPlay.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				audioPlayer.start();
			}
        	
        });
        
        jButtonStop.setIcon(new ImageIcon(getClass().getResource("/agenda/Player Stop.png"))); // NOI18N
        jButtonStop.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				audioPlayer.stop();
				audioPlayer.close();
				iniciaAudioPlayer();
			}
        	
        });
        jButtonPause.setIcon(new ImageIcon(getClass().getResource("/agenda/Player Pause.png"))); // NOI18N
        jButtonPause.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				audioPlayer.stop();
			}
        	
        });
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jButtonPlay, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonPause, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonStop, 0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabelNome)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelAnotação, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Nome, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelAnotação)
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNome)
                    .addComponent(Nome, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonPause)
                    .addComponent(jButtonPlay, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonStop))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    // Variables declaration - do not modify
    private javax.swing.JButton jButtonPause;
    private javax.swing.JButton jButtonPlay;
    private javax.swing.JButton jButtonStop;
    private javax.swing.JLabel jLabelAnotação;
    private javax.swing.JLabel jLabelNome;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel Nome;
    private static Anotação Anotacao;
    private static javax.media.Player audioPlayer = null;
    // End of variables declaration
    static void iniciaAudioPlayer(){
    	try{
    		audioPlayer = Manager.createPlayer(Anotacao.getFile().toURI().toURL());
    	} catch(Exception e){
    		final JFrame Erro = new JFrame();
		    Erro.setLocationRelativeTo(null);  
			Erro.setSize(200,100);
			JPanel Painel = new JPanel();
			JLabel Mensagem = new JLabel("Arquivo deve ser MP3");
			JButton Ok = new JButton("Ok");
			Ok.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Erro.setVisible(false);
					Erro.dispose();
				}
				
			});
			Painel.add(Mensagem);Painel.add(Ok);Erro.add(Painel);
			Erro.setVisible(true);
    	}
    	
    }
}
